/*
 * Copyright (C) GVoiceMail 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.boxandroid.gvoicemail;

import java.io.IOException;
import java.util.Date;

import greendroid.app.GDActivity;
import greendroid.graphics.drawable.ActionBarDrawable;
import greendroid.widget.ActionBarItem;
import greendroid.widget.NormalActionBarItem;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.boxandroid.gvoicemail.media.AudioRecorder;

public class GVoiceMail extends GDActivity implements OnClickListener {

	/** TEMP */
	public static final String TEMP_PATH = "GvoiceMail/temp/"
			+ new Date().getTime();
	/** Button rec */
	private Button rec;
	/** is recording */
	private boolean recording = false;
	/** Record file */
	public final AudioRecorder audioRec = new AudioRecorder(TEMP_PATH);

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.main);

		addActionBarItem(
				getActionBar().newActionBarItem(NormalActionBarItem.class)
						.setDrawable(
								new ActionBarDrawable(getResources(),
										R.drawable.gd_action_bar_export)),
				R.id.item_action_bar_send);

		rec = (Button) findViewById(R.id.rec_button);
		rec.setOnClickListener(this);
	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		switch (item.getItemId()) {
		case R.id.item_action_bar_send:
			sendEmail();
			return true;
		default:
			return super.onHandleActionBarItemClick(item, position);
		}
	}

	public boolean isRecording() {
		return recording;
	}

	private void sendEmail() {
		if (audioRec.path.contains(".mp3")) {
			final Intent emaili = new Intent(Intent.ACTION_SEND);
			emaili.setType("audio/mp3");
			emaili.putExtra(Intent.EXTRA_EMAIL,
					new String[] { "aderbalnunes@gmail.com" });
			emaili.putExtra(Intent.EXTRA_SUBJECT, "New Voice Mail");
			emaili.putExtra(Intent.EXTRA_STREAM,
					Uri.parse("file://" + audioRec.path));
			emaili.putExtra(Intent.EXTRA_TEXT, "Sent by GVoice Mail");
			startActivity(Intent.createChooser(emaili, "Send email..."));
		} else {
			Toast.makeText(this, getString(R.string.mas_no_record),
					Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();
		getWindow().setFormat(PixelFormat.RGBA_8888);
	}

	public void onClick(View view) {
		if (view.getId() == rec.getId()) {
			record();
		}
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		safeClose();
	}

	private void record() {
		try {
			if (!isRecording()) {
				rec.setBackgroundResource(R.drawable.mic_recording);
				audioRec.start();
				Toast.makeText(this, getString(R.string.msg_start_rec),
						Toast.LENGTH_LONG).show();
			} else {
				rec.setBackgroundResource(R.drawable.mic_default);
				audioRec.stop();
				Toast.makeText(this, getString(R.string.msg_send_rec),
						Toast.LENGTH_LONG).show();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		recording = !recording;
	}
	
	private void safeClose(){
		if(isRecording()){
			try {
				audioRec.stop();
				audioRec.removeCache();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}		
	}
}